package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author liuyj
 * @Title: XMLMapperBuilder
 * @create 2020-05-28 10:59
 * @ProjectName lagou_project
 * @Description: 解析mapper.xml文件
 */
public class XMLMapperBuilder {
    private Configuration configuration;
    public XMLMapperBuilder(Configuration configuration) {
        this.configuration=configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> list = rootElement.selectNodes("//select");
        for (Element element : list) {

            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sqlText = element.getTextTrim();

            String key=namespace+"."+id;
            MappedStatement mappedStatement=new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamenterType(parameterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        //insert标签
        List<Element> insertList = rootElement.selectNodes("//insert");
        for (Element element : insertList) {

            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sqlText = element.getTextTrim();

            String key=namespace+"."+id;
            MappedStatement mappedStatement=new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamenterType(parameterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
        //update标签
        List<Element> updateList = rootElement.selectNodes("//update");
        for (Element element : updateList) {

            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sqlText = element.getTextTrim();

            String key=namespace+"."+id;
            MappedStatement mappedStatement=new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamenterType(parameterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        //delete标签
        List<Element> deleteList = rootElement.selectNodes("//delete");
        for (Element element : deleteList) {

            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sqlText = element.getTextTrim();

            String key=namespace+"."+id;
            MappedStatement mappedStatement=new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamenterType(parameterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }
}
